package com.weng.platform.platform.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.cloud.utils.CloudAuthUtil;
import com.weng.framework.common.model.component.option.SelectOption;
import com.weng.framework.core.constants.ResultCodeEnum;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.platform.entity.Column;
import com.weng.platform.platform.model.vo.ColumnTreeNode;
import com.weng.platform.platform.model.vo.ColumnVo;
import com.weng.platform.platform.service.IColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 公共栏目管理 前端控制器
 *
 * @author wenghui
 * @date 2019-11-29 14:48:01
 *
 */
@Slf4j
@RestController
@RequestMapping("/column")
@Api(value = "栏目api", tags = {"栏目操作接口"})
public class ColumnApi extends BaseController {
		@Autowired
		private IColumnService columnService;

		/*
		 *
		 * @param [type: 类型,minder思维导图，draw画面，article文章]
		 * @return
		 */
		@ResponseBody
		@RequestMapping( value = "/getTreeGridData" ,method=RequestMethod.GET)
		@ApiOperation(value = "获取树形表格数据", notes = "获取树形表格数据")
		public ResponseData getTreeGridData(String name,String code,String parentId,String isExpand
						, String type, HttpServletRequest request) throws Exception{
				String ownerId = CloudAuthUtil.getCurUserId(request);
				List<ColumnTreeNode> treeData = this.columnService.getTreeGridData(name, code, parentId, isExpand, type, ownerId);
				return ResponseData.success(treeData);
		}

		/**
		 * 获取下拉框
		 */
		@ApiOperation(value = "获取下拉框", notes = "")
		@GetMapping(value = "/select-option")
		public ResponseData selectOption(String name, HttpServletRequest request){
				String ownerId = CloudAuthUtil.getCurUserId(request);
				IPage<Column> iPage = columnService.list(1, 1000,ownerId);
				List<Column> columnList = iPage.getRecords();
				List<SelectOption> options = new ArrayList<>();
				if(columnList != null){
						for(Column column : columnList){
								SelectOption option = new SelectOption(column.getId(), column.getName());
								options.add(option);
						}
				}
				return success(options);
		}

		/**
		 * 功能描述:单个获取栏目
		 */
		@ApiOperation(value = "获取栏目", notes = "")
		@GetMapping(value = "/get")
		public ResponseData get(String id){
				Column column = columnService.getById(id);
				return success(column);
		}


		/**
		 * 条件 分页查询栏目
		 */
		@ApiOperation(value = "分页获取栏目", notes = "")
		@GetMapping(value = "/list")
		public ResponseData list(Integer pageNo, Integer pageSize, HttpServletRequest request){
			String ownerId = CloudAuthUtil.getCurUserId(request);
			IPage<Column> iPage = columnService.list(pageNo, pageSize, ownerId);
				return success(iPage);
		}


		/**
		 * 新增栏目
		 */
		@ApiOperation(value = "新增栏目", notes = "")
		@PostMapping(value = "/add")
		public ResponseData add(@Valid @RequestBody ColumnVo columnReq, HttpServletRequest request){
				Column column = Convert.convert(Column.class, columnReq);
				String userId = CloudAuthUtil.getCurUserId(request);
				String userName = CloudAuthUtil.getCurUserTrueName(request);
				column.setOwnerId(userId);
				column.setOwnerName(userName);
				if(column.getPosition() == null){
						column.setPosition(3.0);
				}
				columnService.add(column);
				return success();
		}

		/**
		 * 更新栏目
		 */
		@ApiOperation(value = "更新栏目", notes = "")
		@PostMapping(value = "/update")
		public ResponseData update(@Valid @RequestBody ColumnVo columnReq){
				Column column = Convert.convert(Column.class, columnReq);

				if(columnReq.getParentId() != null && columnReq.getParentId().equals(columnReq.getId())){
						return ResponseData.failed(ResultCodeEnum.PARAM_VALID_FAIL.getCode(),"不能选择当前结点做为父结点！");
				}
				int flag = columnService.update(column);
			return success(flag);
		}


		/**
		 * 删除栏目
		 */
		@ApiOperation(value = "ids 删除栏目", notes = "可以删除多个，中间以,号分隔")
		@PostMapping(value = "/delete")
		public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
				String ids = deleteReq.getIds();
				if(StringUtils.isEmpty(ids)){
						return failed("删除失败,id不能为空！");
				}
				boolean flag = columnService.removeByIds(Arrays.asList(ids.split(",")));
				if(flag){
						return success(deleteReq.getIds());
				}else{
						return failed("删除失败");
				}
		}
}
